<?php

namespace Home\Controller;

class TaskController extends HomeController {
    
	//protected $config = array('app_type' => 'common', 'read' => 'let_me_do,accept,reject,save_log,save_product,delete_product_log,edit_product_log,in_storehouse,folder');

	//过滤查询字段
	function _search_filter(&$map) {
	    
		$map['is_del'] = array('eq', '0');
		if (!empty($_REQUEST['keyword']) && empty($map['64'])) {
			$map['name'] = array('like', "%" . $_POST['keyword'] . "%");
		}
	}
    
	public function index() {
	    
		$this -> redirect('folder', array('fid' => 'all'));
	}

	/*--------------------------------------------------------------------
	 * 任务状态说明
	 * 0 :未处理
	 * 10:进行中
	 * 20:已完成
	 * 21:已转交
	 * 22:已拒绝
	 --------------------------------------------------------------------*/
	public function folder() {
		
		$plugin['date'] = true;
		$this -> assign("plugin", $plugin);
		$this -> assign('auth', $this -> config['auth']);
		$this -> assign('user_id', get_user_id());

		$where = $this -> _search();
		if (method_exists($this, '_search_filter')) {
			$this -> _search_filter($where);
		}

		$no_finish_task_count = badge_count_no_finish_task();
		$dept_task_count = badge_count_dept_task();
		$no_assign_task_count = badge_count_no_assign_task();

		$this -> assign('no_finish_task_count', $no_finish_task_count);
		$this -> assign('dept_task_count', $dept_task_count);
		$this -> assign('no_assign_task_count', $no_assign_task_count);

		$fid = $_GET['fid'];
		$this -> assign("fid", $fid);

		switch ($fid) {
			
			case 'all' :
				$this -> assign("folder_name", '所有任务');
				break;
				
			case 'dept' :
				$this -> assign("folder_name", '我们部门的任务');
				$auth = $this -> config['auth'];

				if ($auth['admin']) {
					$where_log['type'] = 2;
					$where_log['status'] = array('eq', '0');
					$where_log['executor'] = get_dept_id();
					$task_list = M("TaskLog") -> where($where_log) -> getField('task_id', TRUE);
					if (empty($task_list)) {
						$where['_string'] = '1=2';
					} else {
						$where['id'] = array('in', $task_list);
					}
				} else {
					$where['_string'] = '1=2';
				}
				break;

			case 'no_assign' :
				$this -> assign("folder_name", '不知道由谁处理的任务');

				$prefix = C('DB_PREFIX');

				$assign_list = M("Task") -> getField('id', true);

				$sql = "select id from {$prefix}task task where status=0 and not exists (select * from {$prefix}task_log task_log where task.id=task_log.task_id)";
				$task_list = M() -> query($sql);

				if (empty($task_list)) {
					$where['_string'] = '1=2';
				} else {
					foreach ($task_list as $key => $val) {
						$list[] = $val['id'];
					}
					$where['id'] = array('in', $list);
				}

				break;

			case 'no_finish' :
				$this -> assign("folder_name", '我未完成');

				$where_log['status'] = array('lt', 20);
				$where_log['executor'] = get_user_id();
				$where_log['type'] = array('eq', 1);

				$task_list = M("TaskLog") -> where($where_log) -> getField('task_id', true);
				if (empty($task_list)) {
					$where['_string'] = '1=2';
				} else {
					$where['id'] = array('in', $task_list);
				}

				break;

			case 'finished' :
				$this -> assign("folder_name", '我已完成');

				$where_log['executor'] = get_user_id();
				$where_log['type'] = array('eq', 1);

				$task_list = M("TaskLog") -> where($where_log) -> getField('task_id', true);
				if (empty($task_list)) {
					$where['_string'] = '1=2';
				} else {
					$where['id'] = array('in', $task_list);
					$where['status'] = array('eq', 30);
				}
				break;

			case 'my_task' :
				$this -> assign("folder_name", '我发布的任务');
				$where['user_id'] = get_user_id();
				break;

			case 'my_assign' :
			    
				$this -> assign("folder_name", '我指派的任务');
				$where_log['assigner'] = get_user_id();
				$task_list = M("TaskLog") -> where($where_log) -> getField('task_id', TRUE);
				if (empty($task_list)) {
					$where['_string'] = '1=2';
				} else {
					$where['id'] = array('in', $task_list);
				}
				break;
			
			case 'my_check' :
				 
				$this -> assign("folder_name", '审核入库');
				$where['is_check_in'] = 1;
				$where['check_user_id'] = get_user_id();
				break;
				
			default :
				break;
		}

		$model = D('Task');
		if (!empty($model)) {
			$this -> _list($model, $where);
		}
		$this->assign('li_name',I('post.li_name',''));
		$this->assign('li_content',I('post.li_content',''));
		$this->assign('eq_user_name',I('post.eq_user_name',''));
		$this->assign('be_create_time',I('post.be_create_time',''));
		$this->assign('en_create_time',I('post.en_create_time',''));
		$this -> display();
	}

	public function edit($id) {
	    
		$plugin['uploader'] = true;
		$plugin['editor'] = true;
		$plugin['date'] = true;
		$this -> assign('plugin', $plugin);
		$model = M('Task');
		
		$task_product = M('task_product') -> where('task_id='.$id) -> field('product,number,product_price,is_check_in,storehouse_product_id') -> select();
		$product = M('storehouse_product')->getField("id,product,product_bh");
		
		$this -> assign("task_product", $task_product);
		$this -> assign('product',$product);
		
		$this -> _edit($id);
	}

	public function del($id) {
		$this -> _del($id);
	}

	public function add() {
		
		$plugin['uploader'] = true;
		$plugin['editor'] = true;
		$plugin['date'] = true;
		$this -> assign("plugin", $plugin);
		$product = M('storehouse_product')->getField("id,product,product_bh");
		$this->assign('product',$product);
		$this -> display();
	}
	
	 /**
	  * 插入(non-PHPdoc)
	  * @see \Home\Controller\HomeController::_insert()
	 */
	public function _insert(){
	    
	   $d = $_POST;
	   $model = D('task');
	   if (false === $model -> create()) {
	       $this -> error($model -> getError());
	   }
	   $list = $model -> add();
	   
	   if ($d['is_check_in'] ==0) {  // 不入库的产品
		   	if (!empty($d['product'])) {
		   		$insert = array();
		   		foreach ($d['product'] as $key=>$val) {
		   			$insert[$key]['task_id'] = $list;
		   			$insert[$key]['user_id'] = get_user_id();
		   			$insert[$key]['user_name'] = get_user_name();
		   			$insert[$key]['product'] = $val;
		   			$insert[$key]['number'] = $d['number'][$key];
		   			$insert[$key]['product_price'] = empty($d['product_price'][$key]) ? 0 : $d['product_price'][$key];
		   			$insert[$key]['update_at'] = date('Y-m-d H:i:s',time());
		   			$insert[$key]['create_at'] = date('Y-m-d H:i:s',time());
		   		}
		   		$task_product = M('task_product');
		   		$task_product->addAll($insert);
		   	}
		   	
	   } else if($d['is_check_in'] ==1) {  //入库的产品
	   		
	   		$map['id'] = array('in',array_values($d['storehouse_product_id']));

	   		$storeProductResult = M('storehouse_product')->where($map)->field('id,product,product_bh,product_type_id,product_type_name')->select();

	   		$storeProduct = array();
	   	    foreach ($storeProductResult as $val) {
	   	    	$storeProduct[$val['id']] = $val['product'];
	   	    } 

	   	    $insert = array();
	   	    foreach ($d['storehouse_product_id'] as $key=>$val) {
	   	    	$insert[$key]['task_id'] = $list;
	   	    	$insert[$key]['user_id'] = get_user_id();
	   	    	$insert[$key]['user_name'] = get_user_name();
	   	    	$insert[$key]['product'] = empty($storeProduct[$val]) ? '' :  $storeProduct[$val];
	   	    	$insert[$key]['number'] = $d['in_number'][$key];
	   	    	$insert[$key]['product_price'] = empty($d['in_product_price'][$key]) ? 0 : $d['in_product_price'][$key];
	   	    	$insert[$key]['is_check_in'] = $d['is_check_in'];
	   	    	$insert[$key]['storehouse_product_id'] = $val;
	   	    	$insert[$key]['update_at'] = date('Y-m-d H:i:s',time());
	   	    	$insert[$key]['create_at'] = date('Y-m-d H:i:s',time());
	   	    }
	   	    
	   	    $task_product = M('task_product');
	   	    $task_product->addAll($insert);
	   	    
	   }
	   
	   if ($d['is_audit'] ==1) {  // 如果需要人审核
		   	$checkArray = explode('|', trim($d['in_check'],';'));
		   	$update['check_user_name'] = $checkArray[0];
		   	$update['check_user_id'] = $checkArray[1];
		   	$update['is_check_in'] = $d['is_check_in'];
		   	$where['id'] = $list;
		   	$updateStatus = $model->where($where)->save($update);
	   }
	  
	   if ($list !== false) {
	       $this -> assign('jumpUrl', get_return_url());
	       $this -> success('新增成功!');
	   } else {
	       $this -> error('新增失败!');
	   }
	}
	
	 /**
	  * 添加产品
	 */
	public function save_product() {
		
		$d = $_POST;
		if (empty($d['task_id'])) {
			$this->output(false,'请选择任务');
		}
		$user_id = get_user_id();
		$task_log = M('task_log');
		$taskLogResult = $task_log->field('assigner,type')->where('task_id = '.$d['task_id'].' AND executor = '.$user_id)->find();
		if (empty($taskLogResult)) {
			$this->output(false,'请选择有效任务');
		}
		if (count($d['product_id'])<=0) {
			$this->output(false,'请添加生产产品');
		}
		if (count($d['number'])<=0) {
			$this->output(false,'请添加生产产品数量');
		}
		if (count($d['number']) != count($d['product_id'])) {
			$this->output(false,'产品和数量不一致');
		}
		$task_product = M('task_product');
		$map['id'] = array('in',implode(',',$d['product_id']));
		$pro = array();
		$prop = array();
		$taskProductLog = $task_product->where($map)->field('id,task_id,product,product_price')->select();
		foreach ($taskProductLog as $k=>$v) {
		    $pro[$v['id']] = $v['product'];
		    $prop[$v['id']] = $v['product_price'];
		}
		$insert = array();
		foreach ($d['product_id'] as $key=>$val) {
			
		    if ($d['number'][$key]<=0) { //如果数量小于零
		    	continue;
		    }
		    $insert[$key]['task_product_id'] = $val; // 发布产品的ID
		    $insert[$key]['dept_id'] = get_dept_id();  //所属部门
			$insert[$key]['user_id'] = get_user_id();
			$insert[$key]['user_name'] = get_user_name();
			$insert[$key]['parent_user_id'] = $taskLogResult['assigner'];
			$insert[$key]['task_id'] = $d['task_id'];
			$insert[$key]['product'] = $pro[$val];
			$insert[$key]['number'] = $d['number'][$key];
			$insert[$key]['product_price'] = $prop[$val];
			$insert[$key]['total_product_price'] = $d['number'][$key]*$prop[$val];
			$insert[$key]['status'] = 2; //默认审核通过
			$insert[$key]['update_at'] = date('Y-m-d H:i:s',time());
			$insert[$key]['create_at'] = date('Y-m-d H:i:s',time());
		}
		$task_product_log = M('task_product_log');
		if (empty($insert)) {
		    $this->output(false,'请添加产品');
		}
		$status = $task_product_log->addAll($insert);
		if ($status) {
			$this->output(true,'添加成功');
		} else {
			$this->output(false,'添加失败,请刷新再试');
		}
	}
	
	
	/**
	 * 更新(non-PHPdoc)
	 * @see \Home\Controller\HomeController::_update()
	 */
	public function _update() {
		
		$d = $_POST;
		if (empty($d['id'])) {
			$this -> error('编辑失败!');
		}
		$model = D('task');
		$task_product = M('task_product');
		if (false === $model -> create()) {
			$this -> error($model -> getError());
		}
		$list = $model -> save();
		
		$task_product -> where('task_id='.$d['id']) -> delete();
		
		if ($d['is_check_in'] ==0) {  // 不入库的产品 
		    if (!empty($d['product'])) {
		        $insert = array();
		        foreach ($d['product'] as $key=>$val) {
		            $insert[$key]['task_id'] = $d['id'];
		            $insert[$key]['user_id'] = get_user_id();
		            $insert[$key]['user_name'] = get_user_name();
		            $insert[$key]['product'] = $val;
		            $insert[$key]['number'] = $d['number'][$key];
		            $insert[$key]['product_price'] = empty($d['product_price'][$key]) ? 0 : $d['product_price'][$key];
		            $insert[$key]['update_at'] = date('Y-m-d H:i:s',time());
		            $insert[$key]['create_at'] = date('Y-m-d H:i:s',time());
		        }
		        $task_product = M('task_product');
		        $task_product->addAll($insert);
		    }
		    
		} else if ($d['is_check_in'] ==1) {  // 入库的产品

		    $map['id'] = array('in',array_values($d['storehouse_product_id']));
		    
		    $storeProductResult = M('storehouse_product')->where($map)->field('id,product,product_bh,product_type_id,product_type_name')->select();
		    
		    $storeProduct = array();
		    foreach ($storeProductResult as $val) {
		        $storeProduct[$val['id']] = $val['product'];
		    }
		    
		    $insert = array();
		    foreach ($d['storehouse_product_id'] as $key=>$val) {
		        $insert[$key]['task_id'] = $d['id'];
		        $insert[$key]['user_id'] = get_user_id();
		        $insert[$key]['user_name'] = get_user_name();
		        $insert[$key]['product'] = empty($storeProduct[$val]) ? '' :  $storeProduct[$val];
		        $insert[$key]['number'] = $d['in_number'][$key];
		        $insert[$key]['product_price'] = empty($d['in_product_price'][$key]) ? 0 : $d['in_product_price'][$key];
		        $insert[$key]['is_check_in'] = $d['is_check_in'];
		        $insert[$key]['storehouse_product_id'] = $val;
		        $insert[$key]['update_at'] = date('Y-m-d H:i:s',time());
		        $insert[$key]['create_at'] = date('Y-m-d H:i:s',time());
		    }
		    	
		    $task_product = M('task_product');
		    $task_product->addAll($insert);
		}
		
		if ($d['is_audit'] ==0) {
			$update['check_user_name'] = '';
			$update['check_user_id'] = 0;
			$update['is_check_in'] = $d['is_check_in'];
			$update['is_audit'] = $d['is_audit'];
			$where['id'] = $d['id'];
			$updateStatus = $model->where($where)->save($update);
		} else {
			$checkArray = explode('|', trim($d['in_check'],';'));
			$update['check_user_name'] = $checkArray[0];
			$update['check_user_id'] = $checkArray[1];
			$update['is_check_in'] = $d['is_check_in'];
			$update['is_audit'] = $d['is_audit'];
			$where['id'] = $d['id'];
			$updateStatus = $model->where($where)->save($update);
		}
		
		if (false !== $list) {
			$this -> assign('jumpUrl', get_return_url());
			$this -> success('编辑成功!');
		} else {
			$this -> error('编辑失败!');
		}
	}
	

	public function read($id) {
		
		$plugin['uploader'] = true;
		$plugin['jquery-ui'] = true;
		$plugin['editor'] = true;
		$plugin['date'] = true;

		$this -> assign("plugin", $plugin);
		$auth = $this -> config['auth'];
		$this -> assign('auth', $auth);

		$this -> assign('task_id', $id);

		$model = M("Task");
		$vo = $model -> find($id);
		$this -> assign('vo', $vo);

		$where_log['task_id'] = array('eq', $id);
		$task_log = M("TaskLog") -> where($where_log) -> select();
		$this -> assign('task_log', $task_log);
		
		if (empty($vo['executor'])) {
			$this -> assign('no_assign', 1);
		}

		$where_accept['status'] = 0;
		$where_accept['task_id'] = $id;
		$where_accept['type'] = 1;
		$where_accept['executor'] = array('eq', get_user_id());
		$task_accept = M("TaskLog") -> where($where_accept) -> find();

		if ($task_accept) {
			$this -> assign('is_accept', 1);
			$this -> assign('task_log_id', $task_accept['id']);
		}

		if ($this -> config['auth']['admin']) {
			$where_dept_accept['status'] = 0;
			$where_dept_accept['task_id'] = $id;
			$where_dept_accept['type'] = 2;
			$where_dept_accept['executor'] = array('eq', get_dept_id());
			$task_dept_accept = M("TaskLog") -> where($where_dept_accept) -> find();
			if ($task_dept_accept) {
				$this -> assign('is_accept', 1);
				$this -> assign('task_log_id', $task_dept_accept['id']);
			}
		}
		$user_id = get_user_id();
		$where_working['status'] = array('in', '0,10');
		$where_working['task_id'] = $id;
		$where_working['type'] = 1;
		$where_working['executor'] = array('eq', $user_id);
		$task_working = M("TaskLog") -> where($where_working) -> find();

		if (empty($task_working) && $auth['write']) {
			$where_working['type'] = 2;
			$where_working['executor'] = array('eq', get_dept_id());
			$task_working = M("TaskLog") -> where($where_working) -> find();
		}

		if ($task_working) {
			$this -> assign('is_working', 1);
			$this -> assign('task_working', $task_working);
		}
		$task_product = M('task_product')->where('task_id='.$id)->field('id,task_id,product,number,product_price,is_check_in,storehouse_product_id,create_at')->select();
		
		$task_product_log = M('task_product_log')->field('id,task_product_id,task_id,parent_user_id,user_name,product,number,product_price,total_product_price,is_in,create_at')->where('task_id='.$id)->order('create_at desc')->select();
		
		$this->assign('task_product',$task_product);
		$this->assign('task_product_log',$task_product_log);
		$this->assign('user_id',$user_id);
		$this -> display();
	}

	function let_me_do($task_id) {
	    
		if (IS_POST) {
			M("Task") -> where("id=$task_id") -> setField('executor', get_user_name() . "|" . get_user_id());
			M("Task") -> where("id=$task_id") -> setField('status', 10);

			$data['task_id'] = I('task_id');
			$data['executor'] = get_user_id();
			$data['executor_name'] = get_user_name();
			$data['transactor'] = get_user_id();
			$data['transactor_name'] = get_user_name();
			$data['status'] = 10;
			$data['type'] = 1;
						
			$task_id = I(task_id);
			$list = M("TaskLog") -> add($data);
			if ($list != false) {
				//$this -> _add_to_schedule($task_id);
				$return['info'] = '接受成功';
				$return['status'] = 1;
				$this -> ajaxReturn($return);
			} else {
				$this -> error('提交成功');
			}
		}
	}

	/**
	 * 保存日志
	 * @param unknown $id
	 */
	public function save_log($id) {
		
	    $model = D("TaskLog");
		if (false === $model -> create()) {
			$this -> error($model -> getError());
		}
		
		$model -> transactor = get_user_id();
		$model -> transactor_name = get_user_name();

		$status = I('status');
		$finish_rate = I('finish_rate');
		$task_log_id = $id;

		if ($finish_rate == '100.00') {
			$model -> finish_time = to_date(time());
			$model -> status = 20;
			$status = 20;
		}
		
		if($model -> status==22) {
			$model -> finish_time = to_date(time());
		}

		$list = $model -> save();

		$task_id = M("TaskLog") -> where("id=$task_log_id") -> getField('task_id');

		if ($status == 10) {
			M("Task") -> where("id=$task_id") -> setField('status', 10);
		}

		if ($status >= 20) {
			
		    $where_total_count['task_id'] = array('eq', $task_id);
			$total_count = M("TaskLog") -> where($where_total_count) -> count();

			$where_finish_count['task_id'] = array('eq', $task_id);
			$where_finish_count['status'] = array('egt', 20);
			$finish_count = M("TaskLog") -> where($where_finish_count) -> count();

			if ($total_count == $finish_count) {
			    
				M("Task") -> where("id=$task_id") -> setField('status', 30);

				$user_id = M('Task') -> where("id=$task_id") -> getField('user_id');
            
				$task = M("Task") -> where("id=$task_id") -> find();
            
				$transactor_name = get_user_name();

				$push_data['type'] = '任务';
				$push_data['action'] = '已完成';
				$push_data['title'] = "{$transactor_name}已完成您发起的[{$task['name']}]任务";
				$push_data['content'] = "如有问题，请与[{$transactor_name}]进行沟通。";
				$push_data['url'] = U('Task/read',"id={$task['id']}&return_url&Task/index");

				send_push($push_data, $user_id);
			}
		}

		if ($status == 21) {
			$task_id = I('task_id');
			$forword_executor = I('forword_executor');
			D('Task') -> forword($task_id, $forword_executor);
		}
		if ($status == 22) {

		}
		if ($list !== false) {
			$this -> assign('jumpUrl', get_return_url());
			$this -> success('提交成功!');
		} else {
			$this -> error('提交失败!');
		}
	}

	function upload() {
		$this -> _upload();
	}

	function down($attach_id) {
		$this -> _down($attach_id);
	}

	private function _add_to_schedule($task_id) {
		$info = M("Task") -> where("id=$task_id") -> find();
		$data['name'] = $info['name'];
		$data['content'] = $info['content'];
		$data['start_time'] = to_date(time());
		$data['end_time'] = $info['expected_time'];
		$data['user_id'] = get_user_id();
		$data['user_name'] = get_user_name();
		$data['priority'] = 3;

		$list = M('Schedule') -> add($data);
	}

	function _send_mail_finish($task_id, $executor) {
		
		$executor_info = M("User") -> where("id=$executor") -> find();
		
		$email = $executor_info['email'];
		$user_name = $executor_info['name'];
		
		$info = M("Task") -> where("id=$task_id") -> find();
		
		$transactor_name = M("TaskLog") -> where("task_id=$task_id") -> getField('id,transactor_name');

		$transactor_name = implode(",", $transactor_name);

		$title = "您发布任务已完成：" . $info['name'];

		$body = "您好，{$user_name}，{$transactor_name} 完成了您发起的[{$info['name']}]任务</br>";
		$body .= "任务主题：{$info['name']}</br>";
		$body .= "任务时间：{$info['expected_time']}</br>";
		$body .= "任务执行人：{$transactor_name}</br>";
		$body .= "请及时检查任务执行情况，如有问题，请与[{$transactor_name}]进行沟通。</br>";
		$body .= "任务完成后请对[任务执行人]表达我们的感谢。</br>";

		$body .= "点击查看任务详情：http://" . $_SERVER['SERVER_NAME'] . U('Task/read', 'id=' . $info['id']) . "</br>";
		$body .= "霞湖世家，感谢有您！</br>";
		//send_mail($email, $user_name, $title, $body);
	}
	
	/**
	 * 删除提交产品任务记录
	 */
	function delete_product_log() {
	    
	   $d = $_POST;
	   if (empty($d['log_id'])) {
	   	   $this->output(false,'请选择删除记录');
	   } 
	   $taskProductLog = M('task_product_log'); 
	   $status = $taskProductLog->where('log_id='.$d['log_id'])->delete();
	   if ($status) {
	       $this->output(true);
	   } else {
	       $this->output(false,'请刷新重试');
	   }
	}
	
	/**
	 * 编辑产品的提交数量
	*/
	function edit_product_log() {
        
	    $d = $_POST;
	    if (empty($d['log_id'])) {
	        $this->output(false,'请选择删除记录');
	    }
	    if (empty($d['number'])) {
	        $this->output(false,'请选择产品数量');
	    }
	    $map['id'] = $d['log_id'];
	    $task = M('task_product_log');
	    $taskProductLog = $task -> where($map) -> field('id,product,number,product_price') -> find();
	    if (empty($taskProductLog)) {
	        $this->output(false,'请选择有效记录');
	    }
	    $data['number'] = $d['number'];
	    $data['total_product_price'] = $d['number']*$taskProductLog['product_price'];
	    $status = $task -> where($map) -> save($data);
	    if ($status) {
	        $this->output(true,'修改成功');
	    } else {
	        $this->output(false,'修改失败，请刷新页面重新再试');
	    }
	}
	
	/**
	 * 入库操作
	 */
	function in_storehouse() {
        
	    $d = $_POST;
	    if (empty($d['log_id'])) {
	        $this->output(false,'请选择删除记录');
	    }
	    $map['id'] = $d['log_id'];
	    $task = M('task_product_log');
	    $product = M('task_product');
	    $storehouse_product = M('storehouse_product');
	    $storehouse = M('storehouse');
	    $taskProductLog = $task -> where($map) -> field('id,task_product_id,number') -> find();
	    if (empty($taskProductLog)) {
	        $this->output(false,'请选择有效记录');
	    }
	    $productMap['id'] = $taskProductLog['task_product_id'];
	    $productLog = $product->where($productMap)->field('id,task_id,is_check_in,storehouse_product_id')-> find();
	    if (empty($productLog)) {
	        $this->output(false,'请选择有效产品');
	    }
	    if ($productLog['is_check_in'] ==0) {
	        $this->output(false,'该产品不需要入库');
	    }
	    
	    $storeHouseProductMap['id'] = $productLog['storehouse_product_id'];
	    $storehouse_product_result = $storehouse_product->where($storeHouseProductMap)->field('id,product,product_bh,product_type_id,product_type_name')-> find();
	    if (empty($storehouse_product_result)) {
	        $this->output(false,'请选择有效产品');
	    }
	    
	    $insert = array(
            'product_id'=>$storehouse_product_result['id'],
            'product_name' => $storehouse_product_result['product'],
            'product_bh' => $storehouse_product_result['product_bh'],
            'product_type_id' => $storehouse_product_result['product_type_id'],
            'product_type_name' => $storehouse_product_result['product_type_name'],
            'number'  => $taskProductLog['number'],
            'type' => 1,
            'user_id' => get_user_id(),
            'user_name' => get_user_name(),
            'opreate_name' => get_user_name(),
            'remark' => '生产任务入库',
            'update_at' => date('Y-m-d H:i:s',time()),
            'create_at' => date('Y-m-d H:i:s',time())
	    );
	    
	    $data['is_in'] = 1;
	    
	    $task->startTrans();
	    $status = $task -> where($map) -> save($data);
	    $flag = $storehouse_product -> where($storeHouseProductMap) -> setInc('num',$taskProductLog['number']);
	    $gg = $storehouse_product -> where($storeHouseProductMap) -> setInc('in_num',$taskProductLog['number']);
	    $hh = $storehouse -> add($insert);
	    if ($status && $flag && $gg && $hh) {
	        $task->commit();
	        $this->output(true,'入库成功');
	    } else {
	        $task->rollback();
	        $this->output(false,'入库失败');
	    }
	    
	}
	
}
